<?php

namespace app\modules\sysConfig\controllers;

use Yii;
use app\modules\sysConfig\models\Sms;
use app\modules\sysConfig\models\search\SmsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \app\modules\sysConfig\models\Smstemplate;
use app\modules\lenders\models\TimeSchedule;

/**
 * SmsController implements the CRUD actions for Sms model.
 */
class SmsController extends Controller {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Sms models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new SmsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    public function actionSearchSend() {
//        $this->layout = '@app/views/layouts/base';
        $searchModel = new SmsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('searchSend', [
                    'model' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 发送短信
     * @return mixed
     */
    public function actionSend() {
        $model = new Sms();
        $searchModel = new SmsSearch();
        $template = Smstemplate::find()->all();
        $dataProvider = $searchModel->searchSms(Yii::$app->request->queryParams);
        if ($model->load(Yii::$app->request->post())) {
            //Yii::$app->user->identity->username
            $time_schedule = new TimeSchedule();
            $time_schedule->isProcessed = 0; //等待发送
            $body = $model->getTemplates($model->smsType)->content;
            $time_schedule->processedArgs = $body;
            $time_schedule->scheduleName = $model->smsType;
            $time_schedule->excuteTime = date('Y-m-d H:i:s');
            $time_schedule->CreateTime = date('Y-m-d H:i:s');
            if ($time_schedule->save()) {
                Yii::$app->session->setFlash('alert', [
                    'options' => ['class' => 'alert-success'],
                    'body' => '发送短信成功！',
                ]);
            } else {
                Yii::$app->session->setFlash('alert', [
                    'options' => ['class' => 'alert-error'],
                    'body' => '发送短信失败！',
                ]);
            }
            return $this->render('send', [
                        'model' => $model,
                        'searchModel' => $searchModel,
                        'template' => $template,
//                        'dataProvider' => $dataProvider,
            ]);
        } else {
            return $this->render('send', [
                        'model' => $model,
                        'searchModel' => $searchModel,
//                        'dataProvider' => $dataProvider,
                        'template' => $template,
            ]);
        }
    }

    /**
     * Displays a single Sms model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Sms model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Sms();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->smsID]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Sms model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->smsID]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Sms model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Sms model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Sms the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Sms::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
